<template>
  <div class="container" @scroll="onScroll" ref="container">
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="detail">
      <h3 class="title">{{ detail.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="detail.aut_photo"
        />
        <div class="text">
          <p class="name">{{ detail.aut_name }}</p>
          <p class="time">{{ detail.pubdate | relTime }}</p>
        </div>
        <van-button @click="toggleFllow" round size="small" type="info">{{
          detail.is_followed ? '已关注' : '+ 关注'
        }}</van-button>
      </div>
      <div class="content" v-html="detail.content"></div>
      <div class="zan">
        <van-button
          @click="toggleLike(1)"
          round
          size="small"
          :class="{ active: detail.attitude === 1 }"
          plain
          icon="like-o"
          >点赞</van-button
        >&nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          @click="toggleLike(0)"
          round
          size="small"
          :class="{ active: detail.attitude === 0 }"
          plain
          icon="delete"
          >不喜欢</van-button
        >
      </div>
      <!-- 评论 -->
      <Comment />
    </div>
  </div>
</template>

<script>
import { getDetailById, disLike, unDisLike, like, unLike } from '@/api/article'
import { follow, unFollow } from '@/api/user'
import Comment from '@/views/article/components/comment'
export default {
  name: 'articleDetail',
  components: {
    Comment
  },
  data () {
    return {
      // 文章详情
      detail: {},
      // 上次看过文章的阅读位置
      scrollTop: 0
    }
  },
  // created () {
  //   console.log('loded')
  // },
  activated () {
    // console.log(this.$route.params)
    if (this.detail.art_id?.toString() !== this.$route.params.id) {
      // 重置阅读位置
      this.scrollTop = 0
      // 获取详情
      this.getDetail()
    } else {
      // 滚到到上次阅读位置
      // this.$nextTick(() => {
      //   this.$refs.container.scrollTop = this.scrollTop
      // })
    }
  },
  methods: {
    // 阅读记忆
    goTop () {
      this.$refs.container.scrollTop = this.scrollTop
    },
    // 获取文章详情
    async getDetail () {
      const { status, data } = await getDetailById(this.$route.params.id)
      if (status === 200) {
        this.detail = data
      }
    },
    // 记录阅读位置
    onScroll (e) {
      // console.log(e.target.scrollTop)
      this.scrollTop = e.target.scrollTop
    },
    // 添加/取消关注
    async toggleFllow () {
      try {
        if (this.detail.is_followed) {
          // 取关
          await unFollow(this.detail.aut_id)

          this.$toast.success('取关成功！')
          this.detail.is_followed = false
        } else {
          // 关注
          await follow(this.detail.aut_id)
          this.$toast.success('关注成功！')
          this.detail.is_followed = true
        }
      } catch (error) {
        this.$toast.fail(error.message)
      }
    },
    // 点赞/不喜欢
    async toggleLike (type) {
      try {
        if (type === 1) {
          // 点赞=》取反
          if (this.detail.attitude === 1) {
            // 取消点赞
            await unLike(this.detail.art_id)
            this.detail.attitude = -1
          } else {
            // 点赞
            await like(this.detail.art_id)
            this.detail.attitude = 1
          }
        } else {
          // 不喜欢=》取反
          if (this.detail.attitude === 0) {
            // 取消不喜欢
            await unDisLike(this.detail.art_id)
            this.detail.attitude = -1
          } else {
            // 不喜欢
            await disLike(this.detail.art_id)
            this.detail.attitude = 0
          }
        }
        // 统一提示
        this.$toast.success('操作成功！')
      } catch (e) {
        this.$toast.fail('操作失败！')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.detail {
  padding: 46px 10px 44px;
  // height: 1000%;
  .title {
    font-size: 18px;
    line-height: 2;
  }
  .zan {
    text-align: center;
    padding: 10px 0;
    .active {
      border-color: red;
      color: red;
    }
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.5;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  // 内容中包含：img 特别宽  code pre 不能换行
  .content {
    padding: 20px 0;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
    /deep/ code {
      white-space: pre-wrap;
    }
    /deep/ pre {
      white-space: pre-wrap;
    }
  }
}
</style>
